DSA Homework 3

Roger Jang


Due date: 20170417 23:59:59

2048 (DFS using a stack)

Outlines

Problem definition

2048 has been a popular game since its debut in March, 2014.

Before you start, you should get familiar with the game. Here is some references:

In this homework, your mission is to write a program to identify an action sequence (consisting of the four actions of east, south, west, north) that can lead the game from its initial map to its desirable final map of at least one element of 2048.

Suggested approach

As instructed in the class, you can use stack-based DFS (depth-first search) to find the action sequence. For details, please refer to the slides and recording of our coverage of the homework.

On the other hand, if you prefer, you can also use recursive backtracking (which uses an implicit stack) to achieve the same goal. (Try google.)

Input/output formats

In an input file, the first line is N, the number of cases of 2048 game for your program to run. And each 4 following lines is an initial map for 2048 game. In other words, there should be 4*N+1 lines in an input file. For example, the following input file has N=10:

10 0 4 2 2 2 0 0 2 0 0 2 0 4 0 2 0 0 2 0 2 2 0 4 2 0 0 0 16 16 0 0 2 0 2 32 4 8 4 16 0 0 0 0 0 2 0 0 0 2 0 0 0 0 0 2 0 0 4 2 0 0 2 16 2 0 0 0 64 2 4 0 0 0 0 8 0 16 0 0 16 2 0 64 4 0 0 2 2 2 0 2 0 2 4 8 2 0 0 0 4 2 0 0 0 2 0 2 0 0 2 0 2 16 2 0 2 4 0 0 2 4 0 0 0 4 0 2 8 0 2 32 8 0 0 2 0 2 2 0 4 4 0 0 0 1024 4 1024 2 4 8 4 8 64 32 64 32 4 2 4 2

For each case, you need to identify the action sequence to generate the final map. If you can find an action sequence to reach the final map (with at least one element of 2048), you should print the action sequence as well as the final map. For instance, a typical output for the first case of the above input file is shown next. (Note that the action and the final map may not be unique.)

Action: 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 1 0 0 1 2 0 3 0 0 1 0 0 0 0 0 0 1 0 1 0 0 1 1 3 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1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 0 3 0 1 0 0 0 1 3 1 0 1 0 1 0 0 1 0 1 1 0 2 1 0 1 0 0 3 0 1 0 1 1 0 0 1 1 3 2 0 1 0 0 0 0 0 1 0 1 1 0 1 3 0 0 0 1 3 2 1 0 1 0 0 0 0 0 0 1 1 0 0 1 0 0 1 2 1 0 0 0 1 0 1 1 2 0 0 0 1 0 0 0 0 0 0 0 1 0 1 3 0 1 1 1 0 0 0 1 2 3 0 3 0 0 2 1 0 3 0 1 2 0 1 1 1 1 1 0 1 3 0 1 0 2 1 0 1 3 1 1 2 3 0 1 0 1 0 2 1 0 1 0 0 3 0 3 3 3 3 2 3 1 2 1 3 0 1 0 1 0 1 0 1 1 2 1 0 Final: 4 32 2 8 256 16 128 2 4 2 8 2048 16 128 16 4 On the other hand, if the desired final map cannot be found (such as the last case of the above input file), then you should print -1 for the action and the final map: Action: -1 Final: -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 An example output file of the above input file will be provided by TA soon.

Requirements

Datasets

  1. Open test sets